<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-form class="input-flex">
          <el-form-item label-width="70px" label="关键字">
            <el-input
              v-model="queryObj.keyword"
              placeholder="根据编号搜索"
              class="el-input-inp"
            >
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="queryObj.keyword = ''"
              >清除</el-button
            >
            <el-button size="small" type="primary" @click="hSearch"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
        <el-alert
          style="margin-bottom: 20px"
          :title="`数据一共${total}条`"
          :closable="false"
          type="info"
          show-icon
        />
        <el-table
          :data="list"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          stripe
          width="100%"
        >
          <el-table-column prop="id" label="编号" width="220px">
          </el-table-column>
          <el-table-column prop="questionType" label="题型" width="70px">
            <template slot-scope="{ row }">
              <span v-if="row.questionType === '1'">单选</span>
              <span v-else-if="row.questionType === '2'">多选</span>
              <span v-else>简答</span>
            </template>
          </el-table-column>
          <el-table-column prop="questionIDs" label="题目编号" width="250px">
            <template slot-scope="{ row }">
              <a
                href="#"
                v-for="(item, index) in row.questionIDs"
                :key="index"
                class="angly"
                @click="Find(item)"
              >
                {{ item.number }}
              </a>
            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="录入时间"> </el-table-column>
          <el-table-column prop="totalSeconds" label="答题时间(s)">
          </el-table-column>
          <el-table-column prop="accuracyRate" label="正确率(%)">
          </el-table-column>
          <el-table-column prop="userName" label="录入人"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button
                @click="hDelete(row)"
                type="button"
                class="el-button el-button--danger el-button--small is-plain is-circle"
                title="删除"
              >
                <i class="el-icon-delete"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="hSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="queryObj.page"
          :page-sizes="[5, 10, 15]"
          :page-size="5"
          layout="->,prev, pager, next,sizes ,jumper"
          :total="total"
        >
        </el-pagination>
      </el-card>
    </div>
    <QuestionsPpreview
      ref="popUp"
      :showDialogVisible.sync="dialogVisible"
      :Id="passId"
    />
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
import QuestionsPpreview from '@/module-hmmm/components/questions-preview'
export default {
  components: {
    QuestionsPpreview
  },
  data () {
    return {
      dialogVisible: false,
      list: [],
      counts: null,
      total: 0,
      queryObj: {
        page: 1,
        pagesize: 5,
        keyword: ''
      },
      passId: '',
      value: ''
    }
  },

  created () {
    this.loadRandoms()
  },
  methods: {
    async hPreview () {
      console.log('弹框出现')
      // const res = await details()
      // console.log(res)
      this.dialogVisible = true
    },
    // 关键字搜索
    async hSearch () {
      this.queryObj.page = 1
      this.loadRandoms()
    },
    // 点击每页显示几条数据
    hSizeChange (pages) {
      this.queryObj.pagesize = pages
      this.loadRandoms()
    },
    // 点击页码显示下一条数据
    handleCurrentChange (page) {
      this.queryObj.page = page
      this.loadRandoms()
    },
    // 数据渲染
    async loadRandoms () {
      const res = await randoms(this.queryObj)
      // 拿到list
      this.list = res.data.items
      // 总条数
      this.total = res.data.counts
      this.queryObj.id = res.data.id
    },
    // 删除数据
    async hDelete (row) {
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await removeRandoms(row)
        this.loadRandoms()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      } catch {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      }
    },
    Find (data) {
      this.dialogVisible = true
      this.passId = data.id
      this.$nextTick(() => {
        console.log('触发了')
        this.$refs.popUp.getInfo()
      })
    }
  }
}
</script>

<style scoped>

.input-flex {
  display: flex;
  justify-content: space-between;
}
.el-input-inp {
  width: 380px;
  height: 20px;
  line-height: 20px;
}
.el-table__header-wrapper {
  background-color: #333;
}
.el-table thead {
  color: #909399;
  font-weight: 500;
}
.angly {
  color: rgb(12, 130, 220);
  display: block;
}
</style>
